package com.example.studentsRoom.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.*;
import com.example.studentsRoom.mapper.*;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BuildAndDormitoryVo {

    /**
     * 楼层查宿舍管理员
     * @param resultPage
     * @param dormitoryAdminMapper
     * @return
     */
    public static PageVo transition(Page<Building> resultPage, DormitoryAdminMapper dormitoryAdminMapper){

        List<Building> buildings = resultPage.getRecords();
        //Vo转换
        List<BuildingVo> voList=new ArrayList<>();
        for (Building building : buildings) {
            BuildingVo buildingVo=new BuildingVo();
            BeanUtils.copyProperties(building,buildingVo);

            //获取宿舍name
            DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectById(buildingVo.getDId());
            buildingVo.setDName(dormitoryAdmin.getDName());

            voList.add(buildingVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setData(voList);
        return pageVo;
    }

    /**
     * 宿舍查楼宇名称
     * @param dormitoryPage
     * @param buildingMapper
     * @return
     */
    public static PageVo getBuild(Page<Dormitory> dormitoryPage, BuildingMapper buildingMapper){
        List<Dormitory> dormitories = dormitoryPage.getRecords();
        //Vo转换
        List<DormitoryVo> voList=new ArrayList<>();
        for (Dormitory dormitory : dormitories) {

            DormitoryVo dormitoryVo=new DormitoryVo();

            BeanUtils.copyProperties(dormitory,dormitoryVo);

            //获取楼宇name
            Building id = buildingMapper.selectById(dormitoryVo.getBId());
            dormitoryVo.setBName(id.getBMsg());

            voList.add(dormitoryVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setTotal(dormitoryPage.getTotal());
        pageVo.setData(voList);
        return pageVo;
    }

    /**
     * 分页查询 缺寝记录
     * @param absentPage
     * @param buildingMapper
     * @param dormitoryAdminMapper
     * @param dormitoryMapper
     * @param studentMapper
     * @param absentMapper
     * @return
     */
    public static PageVo getAbsent(Page<Absent> absentPage,
                                   BuildingMapper buildingMapper,
                                   DormitoryAdminMapper dormitoryAdminMapper,
                                   DormitoryMapper dormitoryMapper,
                                   StudentMapper studentMapper,
                                   AbsentMapper absentMapper
    ){
        List<Absent> resultPage=absentPage.getRecords();

        List<AbsentVo> absentVoList=new ArrayList<>();

        for (Absent record : resultPage) {
            AbsentVo absentVo=new AbsentVo();
            BeanUtils.copyProperties(record,absentVo);

            absentVo.setBMsg(buildingMapper.selectById(record.getBId()).getBMsg());
            absentVo.setDName(dormitoryAdminMapper.selectById(record.getDId()).getDName());
            absentVo.setStuName(studentMapper.selectById(record.getStuId()).getStuName());
            absentVo.setDorName(dormitoryMapper.selectById(record.getDorId()).getDorName());

            absentVoList.add(absentVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setTotal(absentPage.getTotal());
        pageVo.setData(absentVoList);
        return pageVo;
    }
}
